 package com.oanda.blackberry.view;
 
 import com.oanda.blackberry.restapi.Account;
 import com.oanda.blackberry.restapi.AccountStatus;
 import com.oanda.blackberry.util.AbstractViewPanel;
 import com.oanda.blackberry.util.DefaultController;
 import com.oanda.blackberry.util.Model;
 import com.oanda.blackberry.util.PropertyChangeEvent;
 import net.rim.device.api.system.Application;
 import net.rim.device.api.ui.Field;
 import net.rim.device.api.ui.FieldChangeListener;
 import net.rim.device.api.ui.Manager;
 import net.rim.device.api.ui.Screen;
 import net.rim.device.api.ui.UiApplication;
 import net.rim.device.api.ui.component.BasicEditField;
 import net.rim.device.api.ui.component.ButtonField;
 import net.rim.device.api.ui.component.EditField;
 import net.rim.device.api.ui.container.MainScreen;
 
 public class AccountStatusView extends MainScreen
   implements AbstractViewPanel
 {
   DefaultController controller;
   ButtonField useThisAccount = new ButtonField("Use this account", 65536L);
 
   public AccountStatusView(DefaultController controller) {
     this.controller = controller;
     setTitle("Account Status");
     this.useThisAccount.setChangeListener(new FieldChangeListener()
     {
       public void fieldChanged(Field field, int context)
       {
         AccountStatusView.this.useThisAccount();
       } } );
   }
 
   private void useThisAccount() {
     AccountStatusView account = this;
     this.controller.useAccount(((EditField)getField(0)).getText());
     this.controller.popScreen(this);
   }
 
   private void setAcountStatus(final AccountStatus account) {
     final AccountStatusView screen = this;
     Runnable runnable = new Runnable() { 
    	 private final AccountStatus _account = account;
       private final AccountStatusView _screen = screen;
 
       public void run() { AccountStatusView.this.deleteAll();
         AccountStatusView.this.add(new EditField("Id:", this._account.getId()));
         AccountStatusView.this.add(new EditField("Balance:", String.valueOf(this._account.getBalance())));
         AccountStatusView.this.add(new EditField("Open orders:", String.valueOf(this._account.getOpen_orders())));
         AccountStatusView.this.add(new EditField("Open trades:", String.valueOf(this._account.getOpen_trades())));
         AccountStatusView.this.add(new EditField("Margin avail:", String.valueOf(this._account.getMargin_avail())));
         AccountStatusView.this.add(new EditField("Margin used:", String.valueOf(this._account.getMargin_used())));
         AccountStatusView.this.add(new EditField("Realized pl:", String.valueOf(this._account.getRealized_pl())));
         AccountStatusView.this.add(new EditField("Untrealized pl:", String.valueOf(this._account.getUnrealized_pl())));
 
         if (Model.getModel().getAccount().getAccountEntrys().length > 1) {
           if (!this._account.getId().equals(Model.getModel().getActiveAccountId())) {
             AccountStatusView.this.add(AccountStatusView.this.useThisAccount);
           }
           AccountStatusView.this.controller.pushScreen(this._screen);
         }
       }
     };
     UiApplication.getUiApplication().invokeLater(runnable);
   }
 
   public void modelPropertyChange(PropertyChangeEvent evt)
   {
   }
 
   public boolean onClose() {
     this.controller.popScreen(this);
     return true;
   }
}

